package com.yks.pls.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

/**
 * docx
 * @ClassName: com.yks.pls.poi.WordTransHtml.java
 * @author: songxulin
 * @date :  2020-12-18 14:40
 * @version V1.0
 */
public class WordTransHtml {

    /**
     * @throws
     * @Title: docxTransHtml
     * @Description: docx 转换 HTML
     * @param: @param inputStream: docx 文件输入流
     * @param: @param outputStream: html 文件输出流
     * @param: @param imageSaveDir：图片路径
     * @return: void
     */
    public static void docxTransHtml(InputStream inputStream, OutputStream outputStream, String imageSaveDir) {
        try {
            XWPFDocument document = new XWPFDocument(inputStream);

            XHTMLOptions options = XHTMLOptions.create();
            options.setExtractor(new FileImageExtractor(new File(imageSaveDir)));
            options.URIResolver(new BasicURIResolver(imageSaveDir));

            org.apache.poi.xwpf.converter.xhtml.XHTMLConverter.getInstance().convert(document, outputStream, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @throws
     * @Title: docxTransHtml
     * @Description: docx 转换 HTML
     * @param: @param inputStream: docx 文件输入流
     * @param: @param outputStream: html 文件输出流
     * @param: @param imageSaveDir：图片路径
     * @return: void
     */
    public static void xlsxTransHtml(InputStream inputStream, OutputStream outputStream, String imageSaveDir) {
        try {
            HSSFWorkbook document = new HSSFWorkbook(inputStream);

            XHTMLOptions options = XHTMLOptions.create();
            options.setExtractor(new FileImageExtractor(new File(imageSaveDir)));
            options.URIResolver(new BasicURIResolver(imageSaveDir));

//            org.apache.poi.xslf.converter (document, outputStream, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void docxAppendImage(InputStream inputStream, OutputStream outputStream, String imageSaveDir) {
        try {
            XWPFDocument document = new XWPFDocument(inputStream);
            final List<XWPFParagraph> paragraphs = document.getParagraphs();
            final XWPFParagraph xwpfParagraph = paragraphs.get(paragraphs.size() - 1);
            final XWPFRun run = xwpfParagraph.createRun();
            run.addPicture(new FileInputStream(new File(imageSaveDir)) , XWPFDocument.PICTURE_TYPE_PNG, "Generated", Units.toEMU(512), Units.toEMU(256));
            document.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String imagePath = "E:\\temp\\0.png";
        String docPath ="E:\\temp\\有棵树Java笔试题.docx";
        String htmlPath = "E:\\temp\\有棵树Java笔试题3.docx";
        File docFile = new File(docPath);
        File htmlFile = new File(htmlPath);
        File imageDir = new File(imagePath);
        // 判断文件夹是否存在，不存在则创建文件夹
        if(!imageDir.exists()){
            imageDir.mkdirs();
        }
        // 判断html 文件是否存在，不存在则创建
        if(!htmlFile.exists()){
            try {
                htmlFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(docFile.exists() && htmlFile.exists()){
            try {
                InputStream inputStream = new FileInputStream(docFile);
                OutputStream outputStream = new FileOutputStream(htmlFile);
                WordTransHtml.docxAppendImage(inputStream, outputStream, imagePath);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    public static void main2(String[] args) {
        // TODO Auto-generated method stub
        String imagePath = "E:\\temp";
        String docPath ="E:\\temp\\有棵树Java笔试题2.docx";
        String htmlPath = "E:\\temp\\有棵树Java笔试题.html";
        File docFile = new File(docPath);
        File htmlFile = new File(htmlPath);
        File imageDir = new File(imagePath);
        // 判断文件夹是否存在，不存在则创建文件夹
        if(!imageDir.exists()){
            imageDir.mkdirs();
        }
        // 判断html 文件是否存在，不存在则创建
        if(!htmlFile.exists()){
            try {
                htmlFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(docFile.exists() && htmlFile.exists()){
            try {
                InputStream inputStream = new FileInputStream(docFile);
                OutputStream outputStream = new FileOutputStream(htmlFile);
                WordTransHtml.docxTransHtml(inputStream, outputStream, imagePath);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
